import React from 'react';   
import {connect} from 'dva';
import {Link, browserHistory} from 'dva/router';
import {Table, Row, Col, Form, Input, Radio, DatePicker, Button, Modal} from 'antd';
const R = require('ramda');
const dateFormat = 'YYYY-MM-DD';
const { RangePicker } = DatePicker;
import Constants  from '../../../utils/Constants';
import RightContacts from "../../../utils/RightConstants";
import WebsiteCompany from '../../../components/customer/components/WebsiteCompany';
import RightBox from "../../../components/commons/RightBox";
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
export default connect(({customerManage, businessAgreement}) => ({customerManage, businessAgreement}))(
    Form.create()(class websiteCompany extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                open: false,
            }
        }

        componentDidMount() {
            this.props.dispatch({
                type: 'customerManage/getWebsiteCompanyList',
                payload: {
                    certificationStatus: 2,
                    page: Constants.DEFAULT_PAGE
                }
            });
        }        

        handleOpenDetail = (record) => {
            this.props.dispatch({
                type: 'customerManage/getWebsiteById', 
                payload: {
                    id: record.id
                }
            });
            this.websiteCompany.handleOpen();
        }

        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.customerManage.page;
            newPage.page = 1;
            let time=values.time;

            this.props.dispatch({
                type: 'customerManage/getWebsiteCompanyList',
                payload: {
                    customerNO: values.customerNO,
                    cstName: values.cstName,
                    certificationStatus: values.certificationStatus,
                    page: newPage,
                    beginTime:R.isNil(time)||R.isEmpty(time)?null:time[0].format(dateFormat),
                    endTime:R.isNil(time)||R.isEmpty(time)?null:time[1].format(dateFormat),
                }
            });
        };


        columns = [
            {title: '客户编码', dataIndex: 'customerno', key: 'customerno'},
            {title: '客户名称', dataIndex: 'companyname', key: 'companyname',},
            {title: '创建时间', dataIndex: 'createtime', key: 'createtime'},
            {title: '操作', key: 'key_oprator',

            render: (text, record, index) => {
                let editButton= <RightBox resource={RightContacts.CUSTOMER.CUSTOMER_MANAGE_CUSTOMER_EDIT} split={false}>
                                    <span><a href="#" onClick={this.handleOpenDetail.bind(this, record)}>{record.certificationStatus == "2" ? "官网企业审核" : "查看"}</a></span>
                                </RightBox>;

                return (<span>{editButton}</span>);
            }
        }
        ];

        render () {
            let dataSouce = R.isNil(this.props.customerManage.dataSource) ? {} : this.props.customerManage.dataSource;
            let formItemLayout = {labelCol: {span: 6}, wrapperCol: {span: 18}};
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let time=values.time;
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.customerManage.page.size,
                total: this.props.customerManage.page.totalRow,
                showSizeChanger: true,
                current: this.props.customerManage.page.page,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.customerManage.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'customerManage/getWebsiteCompanyList',
                        payload: {
                            customerNO: values.customerNO,
                            cstName: values.cstName,
                            certificationStatus: values.certificationStatus,
                            page: newPage,
                            beginTime:R.isNil(time)||R.isEmpty(time)?null:time[0].format(dateFormat),
                            endTime:R.isNil(time)||R.isEmpty(time)?null:time[1].format(dateFormat),
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.customerManage.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'customerManage/getWebsiteCompanyList',
                        payload: {
                            customerNO: values.customerNO,
                            cstName: values.cstName,
                            certificationStatus: values.certificationStatus,
                            page: newPage,
                            beginTime:R.isNil(time)||R.isEmpty(time)?null:time[0].format(dateFormat),
                            endTime:R.isNil(time)||R.isEmpty(time)?null:time[1].format(dateFormat),
                        }
                    });
                }
            }
            return (
                <div>

                <Row>
                    <Col span={7}><FormItem {...formItemLayout} label={'客户代码'}>{getFieldDecorator('customerNO')(<Input/>)}</FormItem></Col>
                    <Col span={7}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input/>)}</FormItem></Col>
                </Row>
                <Row>
                    <Col span={7}>
                        <FormItem {...formItemLayout} label={'创建时间'}>
                            {getFieldDecorator('time', {})(
                                <RangePicker
                                    style={{width: '100%'}}
                                    showTime={{ format: 'YYYY-MM-DD' }}
                                    format="YYYY-MM-DD"
                                    placeholder={['开始时间', '结束时间']}
                                />
                            )}
                        </FormItem>
                    </Col>

                </Row>
                <Row>
                    <Col span={7}>
                    <FormItem {...formItemLayout} label={'状态'}>
                    {getFieldDecorator('certificationStatus', {
                            initialValue: "2"
                        })(

                            <RadioGroup style={{width: 800}}>
                                <Radio value="2">待审核</Radio>
                                <Radio value="4">审核通过</Radio>
                                <Radio value="5">审核不通过</Radio>
                            </RadioGroup>
                        )}
                        </FormItem>
                    </Col>
                    <Col span={7} style={{float: 'right'}}>
                        <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this)}>查询</Button>
                    </Col>
                </Row>

                <Table rowkey="id" dataSource={this.props.customerManage.WebsiteCompanyList} columns={this.columns} bordered={true} size={'middle'} pagination={pagination}/>
                <WebsiteCompany wrappedComponentRef={(inst) => this.websiteCompany = inst} handleSearch = {this.handleSearch} 
                                websiteCompanyData={this.props.customerManage.websiteCompanyData ? this.props.customerManage.websiteCompanyData : null} 
                                attachments={this.props.customerManage.attachments ? this.props.customerManage.attachments : null} 
                />
            </div>
            )
        }
    })


)
